import { _decorator, Component, instantiate, Label, Node, Prefab, Sprite } from 'cc';
import { LocationType, Soldier } from '../entity/Soldier';
import { SoldierCover } from '../train/SoldierCover';
import VillageApi from '../api/VillageApi';
import { EventType } from '../settings/const';
import { TrainEvent } from '../entity/TrainEvent';
import { SoldierTrainItem } from '../train/SoldierTrainItem';
import { EventManager } from '../event/EventManager';
import { UserInfo } from '../common/UserInfo';
import { PrefabEnum } from '../common/PrefabEnum';
import { UIManager } from '../common/UIManager';
const { ccclass, property } = _decorator;

@ccclass('PalaceTrain')
export class PalaceTrain extends Component {
    @property(Node)
    coverContent:Node
 
    @property(Node)
    trainListContent: Node

    @property(Prefab)
    soldierTrainItem: Prefab

    @property(Prefab)
    soldierCover: Prefab

    start() {
      

       this.init()
    }   

    async init(){
        await UserInfo.instance.refreshVillageDetail()
        this.renderSoldierList()
        
    }

   
 

    protected onDisable(): void {
       
    
        UIManager.instance.closeUI(PrefabEnum.ProPanel)
    }

    renderSoldierList(){
        let list = Soldier.createByLocation(LocationType.Palace)
        console.log(list)
        this.coverContent.destroyAllChildren()
        for(let item of list){
            let node = instantiate(this.soldierCover)
            this.coverContent.addChild(node)
            node.getComponent(SoldierCover).setData(item)
        }
    }

    async renderTrainList() {
        this.trainListContent.destroyAllChildren()

        let res = await VillageApi.getVillageEvents(EventType.train)
        console.log(res)
        let tranEventList: TrainEvent[] = res.data
        for (let e of tranEventList) {
            let node = instantiate(this.soldierTrainItem)
            this.trainListContent.addChild(node)
            node.getComponent(SoldierTrainItem).setData(e)
        }



    }
}


